﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LibraryONTrack.dao
{
	public abstract class BaseDAO:  IDisposable	
	{
		private bool _disposed;

		// SharedSqlDao Singleton
		private SqlDao _sqlDaoSingleton;
		public SqlDao SqlDao
		{
			get
			{
				if (_sqlDaoSingleton == null)
				{
					_sqlDaoSingleton = new SqlDao();
				}
				return _sqlDaoSingleton;
			}
			set
			{
				_sqlDaoSingleton = value;
			}
		}


		// Dispose(bool disposing) executes in two distinct scenarios.
		// If disposing equals true, the method has been called directly
		// or indirectly by a user's code. Managed and unmanaged resources
		// can be disposed.
		// If disposing equals false, the method has been called by the 
		// runtime from inside the finalizer and you should not reference 
		// other objects. Only unmanaged resources can be disposed.
		protected virtual void Dispose(bool disposing)
		{
			// Check to see if Dispose has already been called.
			if (!this._disposed)
			{
				// If disposing equals true, dispose all managed 
				// and unmanaged resources.
				if (disposing)
				{
					// Dispose managed resources 
					// <here>
				}
				// Release unmanaged resources. If disposing is false, 
				// only the following code is executed.
				// <here>

				// Note that this is not thread safe.
				// Another thread could start disposing the object
				// after the managed resources are disposed,
				// but before the disposed flag is set to true.
				// If thread safety is necessary, it must be
				// implemented by the client.

			}
			_disposed = true;
		}

		~BaseDAO()
		{
			// Do not re-create Dispose clean-up code here.
			// Calling Dispose(false) is optimal in terms of
			// readability and maintainability.
			Dispose(false);
		}

		// Implement IDisposable.
		// Do not make this method virtual.
		// A derived class should not be able to override this method.
		public void Dispose()
		{
			Dispose(true);
			// Take yourself off the Finalization queue 
			// to prevent finalization code for this object
			// from executing a second time.
			GC.SuppressFinalize(this);
		}
	}
}
